﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using Ent;
using System.Text.RegularExpressions;
namespace Thi
{
    public partial class frmDangKyThi : DevComponents.DotNetBar.OfficeForm
    {
        string Ma = "";
        public frmDangKyThi(string ma)
        {
            Ma = ma;
            InitializeComponent();
        }
        List<MonHocTbl> dsmon = new List<MonHocTbl>();
        List<DeThiTbl> DSDEthi = new List<DeThiTbl>();
        List<CauHoiThi> LstCauHoi = new List<CauHoiThi>();
        List<TraLoiThi> LstDapAn = new List<TraLoiThi>();
        List<List<CauHoiThi>> ALLCAUHOI = new List<List<CauHoiThi>>();
        private void frmDangKyThi_Load(object sender, EventArgs e)
        {
            ThiSinhBLL tsbll = new ThiSinhBLL();
            ThisinhTbl ts = tsbll.ThongTinThiSinh(Ma);
            txtMSSV.Text = ts.MSSV;
            txtHoTen.Text = ts.TenSV;

            MonHocBLL mh = new MonHocBLL();
            dsmon = new List<MonHocTbl>();
            cboMon.DataSource = null;
            dsmon = mh.LayMonHoc_MaDethi(txtMSSV.Text);
            cboMon.DataSource = dsmon;
            cboMon.DisplayMember = "TenMonHoc";
            cboMon.ValueMember = "MaMonHoc";
            if (cboMon.Items.Count>0)
            {
                cboMon.SelectedIndex = 0;
                LoadDeThi();
            }
        }
        public void LoadDeThi()
        {

            DSDEthi = new List<DeThiTbl>();
            DeThiBLL dtsthi = new DeThiBLL();
            DSDEthi = dtsthi.LayDanhSachDeThi_MaMonHoc(((MonHocTbl)cboMon.SelectedItem).MaMonHoc, txtMSSV.Text);
            cboDe.DataSource = null;
            cboDe.DataSource = DSDEthi;
            cboDe.DisplayMember = "TENDETHI";
            cboDe.ValueMember = "MADETHI";
        }

        private void cboMon_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDeThi();
        }

        private void cboDe_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (DeThiTbl i in DSDEthi)
            {
                if (i.TENDETHI == cboDe.Text)
                {
                    ChiTietDeThiBLL ctdtbll = new ChiTietDeThiBLL();

                    LstCauHoi = ctdtbll.Laydanhsachcauhoi(i.MADETHI);

                }
            }
            if (cboDe.Text.Trim().Length > 0)
            {
                string made = "";
                foreach (DeThiTbl i in DSDEthi)
                {
                    if (cboDe.Text == i.TENDETHI)
                        made = i.MADETHI;
                }
                if (int.Parse(txtSoLgDe.Text) > 0)
                {
                    BusinessLogicLayer bll = new BusinessLogicLayer();
                    ChiTietDeThiBLL ctdtbll = new ChiTietDeThiBLL();
                    cboRandomDe.Items.Clear();
                    ALLCAUHOI.Clear();

                    for (int i = 1; i <= int.Parse(txtSoLgDe.Text); i++)
                    {

                        cboRandomDe.Items.Add("Đề số " + i);
                        List<CauHoiThi> ds1 = new List<CauHoiThi>();
                        ds1 = bll.TronDanhSachCauHoi(LstCauHoi);
                        ALLCAUHOI.Add(ds1);
                    }
                }
                Random rd = new Random();
                cboRandomDe.SelectedIndex = rd.Next(0, 4);
            }
        }

        private void btnThi_Click(object sender, EventArgs e)
        {
            if (Check() == true)
            {
                string temp = "";
                foreach (DeThiTbl i in DSDEthi)
                {
                    if (i.TENDETHI == cboDe.Text)
                        temp = i.MADETHI;
                }
                MatchCollection m = Regex.Matches(cboRandomDe.Text, " ");
                int xy = int.Parse(cboRandomDe.Text.Substring(m[1].Index));
                MonHocBLL mhbll = new MonHocBLL();
                BusinessLogicLayer bll = new BusinessLogicLayer();
                LstDapAn = bll.DanhsachDapAn_TheoListCauHoi(LstCauHoi);
                LstDapAn = bll.TronDanhSachDapAn(LstDapAn);
                frmThi frm = new frmThi(temp, txtMSSV.Text, txtHoTen.Text, ALLCAUHOI[xy - 1], LstDapAn, cboMon.SelectedValue.ToString());
                frm.Show();
                this.Hide();
            }
        }
        public bool Check()
        {
            if (txtHoTen.Text == "" || txtMSSV.Text == "" || cboDe.Text == "" || cboRandomDe.Text == "" || txtSoLgDe.Text == "")
            {
                MessageBox.Show("Dữ liệu trống");
                return false;
            }
            return true;
        }

        private void cboMon_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }
        
    }
}